package gui.common.detail;

import gui.ButtonName;
import gui.GuiCommon;
import gui.client.listener.ClientButtonListener;
import gui.server.listener.ServerButtonListener;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import data.Base;
import data.Forum;

public class ForumDetail extends DetailPage{

	private Forum data=null;
	private boolean isNew=false;
	
	private JTextField txtName;
	private JTextField txtUser;
	private JTextArea txtDes;
	private JButton btnSaveForum;
	private JButton btnClose;
	
	
	
	public ForumDetail(boolean isNew, Base data,boolean isServer) {
		super(isServer);
		this.isNew=isNew;
		if(isNew){
			this.setTitle("Create forum");
		}else{
			this.setTitle("Forum Detail");
		}
		this.data=(Forum)data;
		if(isServer){
			al=new ServerButtonListener(this);
		}else{
			al=new ClientButtonListener(this);
		}
		initUI();
		setEvent();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	private void initUI(){
		GuiCommon.setWindowSize(this, 240, 180);
		
		JLabel subject=new JLabel("Subject");
		JLabel message=new JLabel("Message");
		JLabel user=new JLabel("User");
		
		txtName=new JTextField();
		txtDes=new JTextArea();
		txtUser=new JTextField(data.getUserName());
		txtUser.setEditable(false);
		
		if(!isNew){
			txtName.setText(data.getSubject());
			txtDes.setText(data.getMessage());
		}
		
		GridLayout layout=new GridLayout(3,1);
		this.setLayout(layout);
		layout=new GridLayout(2,2);
		layout.setVgap(5);
		layout.setHgap(5);
		JPanel up=new JPanel(layout);
		layout.setVgap(5);
		layout.setHgap(5);
		layout=new GridLayout(1,2);
		JPanel middle=new JPanel(layout);
		layout=new GridLayout(3,2);
		JPanel down=new JPanel(layout);
		btnSaveForum=new JButton(ButtonName.CREATE_FORUM);
		
		btnClose=new JButton("Close");
		btnClose.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
			dispose();
			
		}});
		up.add(user);
		up.add(txtUser);
		up.add(subject);
		up.add(txtName);
		middle.add(message);
		middle.add(txtDes);
		down.add(new JPanel());
		down.add(new JPanel());
		down.add(btnSaveForum);
		down.add(btnClose);
		
		this.add(up);
		this.add(middle);
		this.add(down);
		
		
	}
	
	private void setEvent(){
		
		btnSaveForum.addActionListener(al);
		btnClose.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Base getOperationResult() {
		data.setMessage(txtDes.getText().trim());
		data.setSubject(txtName.getText().trim());
		return data;
	}
	
}
